package com.example.bgmachine.bgmachine.meidas;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.example.bgmachine.bgmachine.R;

import java.io.File;
import java.util.TreeSet;

public class MediaActivity extends AppCompatActivity {

    private Context mContext = null;
    String mdirName = "";

    private Button media_video_btn_back = null;
    private CustomVideoView mCustomVideoView = null;
    private TreeSet<String> msetcontents = new TreeSet<>();
    private final  String TAG="MediaActivity";
    private  int mindex =0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("aaa", "handleMessage: MediaActivity=3");
        setContentView(R.layout.activity_media);
        getSupportActionBar().hide();
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window myWindow = this.getWindow();
        myWindow.setFlags(flag, flag);
        Log.d("aaa", "handleMessage: MediaActivity=4");
        hideBottomUIMenu();
        Log.d("aaa", "handleMessage: MediaActivity=5");
        media_video_btn_back = findViewById(R.id.media_video_btn_back);
        Log.d("aaa", "handleMessage: MediaActivity=6");
        mCustomVideoView  = findViewById(R.id.media_video_view);
        Log.d("aaa", "handleMessage: MediaActivity=7");
        mContext = this;


        Log.d(TAG, "onCreate:MediaActivity =0");
        //判断SD卡是否存在 getExternalStorageDirectory
        if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            Log.d(TAG, "onCreate:MediaActivity =1");
            mdirName =  Environment.getExternalStorageDirectory().getAbsolutePath()
                    + File.separator+"twbefile"+File.separator;
            File file = new File(mdirName);
            Log.d(TAG, "onCreate:MediaActivity =2="+mdirName);
            if(!file.exists()) {
                Log.d(TAG, "onCreate:MediaActivity =3");
                file.mkdir();
            }

        }

        media_video_btn_back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                finish();

            }
        });
        mCustomVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                handlPlayPath();


            }
        });
        Log.d("aaa", "handleMessage: MediaActivity=8");
        findDir();
        Log.d("aaa", "handleMessage: MediaActivity=9");
        handlPlayPath();
        Log.d("aaa", "handleMessage: MediaActivity=10");


    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        });
    }

    public void findDir(){
        Log.d(TAG, "findDir: =0");
        if(mdirName.length() > 0){
            Log.d(TAG, "findDir: =1");
            File file = new File(mdirName);
            if(file.exists()) {
                Log.d(TAG, "findDir: =2");
                getFiles(mdirName);
            }

        }

    }
    private void getFiles( String path) {
        Log.d("aaa", "getFiles: path=="+path);
        File[] allFiles = new File(path).listFiles();
        for (int i = 0; i < allFiles.length; i++) {
            File file = allFiles[i];
            if (file.isFile()) {
                if(file.getAbsolutePath().contains("mp4")){
                    msetcontents.add(file.getAbsolutePath());
                }

            } else if (!file.getAbsolutePath().contains(".thumnail")) {
                // getFiles( file.getAbsolutePath());
            }
        }
    }

    public void  handlPlayPath(){
//        mCustomVideoView.stopPlayback();
//        mCustomVideoView.clearFocus();
//        mCustomVideoView.setVideoURI(Uri.parse("android.resource://com.example.controltimeviewr.controltimeview/" + R.raw.demo));
//
//        mCustomVideoView.start();
//        if(true){
//            return;
//        }
        Log.d(TAG, "handlPlayPath: =0");
        if(msetcontents.size() > 0){
            Log.d(TAG, "handlPlayPath: =1");
            if( mindex >= msetcontents.size()){
                Log.d(TAG, "handlPlayPath: =2");
                mindex = 0;
            }
            Log.d(TAG, "handlPlayPath: =mindex="+String.valueOf(mindex));
            int indexa = 0;
            boolean isHave =false;
            for (String value : msetcontents ) {
                Log.d("aaaaa", "onCompletion =mdirName + value=a=" + value);
                File filea = new File( value);
                if(mindex == indexa && filea.exists()){
                    Log.d("aaaaa", "onCompletion =mdirName + value="+ value);
                    mCustomVideoView.stopPlayback();
                    mCustomVideoView.clearFocus();
                    mCustomVideoView.setVideoPath( value);
                    mCustomVideoView.start();
                    isHave =true;
                    break;
                }
                indexa++;
            }

            if(!isHave){
                Log.d(TAG, "handlPlayPath: +temnp=3");
                mCustomVideoView.stopPlayback();
                mCustomVideoView.clearFocus();
                // String fileName = getResources().getResourceName(R.raw.demo);
                mCustomVideoView.setVideoURI(Uri.parse("android.resource://com.example.bgmachine.bgmachine/" + R.raw.demo));
                //  Log.d("aaa", "handlPlayPath: fileName=="+fileName);
                //  mCustomVideoView.setVideoURI(Uri.parse(fileName));

                mCustomVideoView.start();
            }else{
                mindex++;
            }

        }else{
            Log.d(TAG, "handlPlayPath: =3");
            mCustomVideoView.stopPlayback();
            mCustomVideoView.clearFocus();
            // String fileName = getResources().getResourceName(R.raw.demo);
            mCustomVideoView.setVideoURI(Uri.parse("android.resource://com.example.bgmachine.bgmachine/" + R.raw.demo));
            //  Log.d("aaa", "handlPlayPath: fileName=="+fileName);
            //  mCustomVideoView.setVideoURI(Uri.parse(fileName));

            mCustomVideoView.start();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

}
